package com.xianyv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xianyv.entity.AdminInfo;
import com.xianyv.entity.AuthUser;
import com.xianyv.mapper.AdminInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 配置数据源，从哪里寻找用户信息
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AdminInfoMapper adminInfoMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        AdminInfo adminInfo = adminInfoMapper.selectOne(new LambdaQueryWrapper<AdminInfo>().eq(AdminInfo::getUsername, username));

        if (adminInfo == null){
            throw new UsernameNotFoundException("不存在该用户，请联系管理员注册");
        }

        return new AuthUser(adminInfo);
    }
}
